package com.thuanpq.database.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.thuanpq.entity.BlacklistItemEntity;

public class BlacklistAdapter {
	private DatabaseAdapter mDbAdapter;

	public BlacklistAdapter(Context context) {
		mDbAdapter = new DatabaseAdapter(context);
	}

	public boolean insertBlacklistItem(BlacklistItemEntity entity) {
		if (entity != null) {
			ContentValues inititalValues = new ContentValues();

			inititalValues.put(BlacklistItemEntity.BI_NAME,
					entity.get_bi_Name());

			inititalValues.put(BlacklistItemEntity.PHONENUMBER,
					entity.get_phoneNumber());

			inititalValues.put(BlacklistItemEntity.IS_BLOCKCALL,
					entity.get_isBlockCall());

			inititalValues.put(BlacklistItemEntity.IS_BLOCKMMS,
					entity.get_isBlockMMS());

			inititalValues.put(BlacklistItemEntity.IS_BLOCKSMS,
					entity.get_isBlockSMS());

			inititalValues.put(BlacklistItemEntity.NOTE, entity.get_note());

			inititalValues.put(BlacklistItemEntity.IS_ENABLE,
					entity.get_isEnable());

			inititalValues.put(BlacklistItemEntity.CREATINGDATE,
					entity.get_creatingDate());

			inititalValues.put(BlacklistItemEntity.LASTINCOMINGCALL,
					entity.get_lastIncomingCall());

			inititalValues.put(BlacklistItemEntity.LASTINCOMINGSMS,
					entity.get_lastIncomingSMS());

			inititalValues.put(BlacklistItemEntity.LASTINCOMINGMMS,
					entity.get_lastIncomingMMS());

			inititalValues.put(BlacklistItemEntity.CANAUTOREPLY,
					entity.get_canAutoReply());

			boolean result = mDbAdapter.insert(BlacklistItemEntity.TABLE_NAME,
					null, inititalValues) > 0;
			return result;
		}
		return false;
	}

	public boolean isBlacklistItem(String phone) {
		Cursor cur = mDbAdapter.query(BlacklistItemEntity.TABLE_NAME, null,
				BlacklistItemEntity.PHONENUMBER + "=?", new String[] { phone },
				null, null, null);

		if (cur != null && cur.getCount() > 0) {
			cur.close();
			return true;
		}

		cur.close();

		return false;
	}

	public boolean updateBlacklistItem(BlacklistItemEntity entity) {
		if (entity != null) {
			ContentValues inititalValues = new ContentValues();

			inititalValues.put(BlacklistItemEntity.BI_NAME,
					entity.get_bi_Name());

			inititalValues.put(BlacklistItemEntity.PHONENUMBER,
					entity.get_phoneNumber());

			inititalValues.put(BlacklistItemEntity.IS_BLOCKCALL,
					entity.get_isBlockCall());

			inititalValues.put(BlacklistItemEntity.IS_BLOCKMMS,
					entity.get_isBlockMMS());

			inititalValues.put(BlacklistItemEntity.IS_BLOCKSMS,
					entity.get_isBlockSMS());

			inititalValues.put(BlacklistItemEntity.NOTE, entity.get_note());

			inititalValues.put(BlacklistItemEntity.IS_ENABLE,
					entity.get_isEnable());

			inititalValues.put(BlacklistItemEntity.CREATINGDATE,
					entity.get_creatingDate());

			inititalValues.put(BlacklistItemEntity.LASTINCOMINGCALL,
					entity.get_lastIncomingCall());

			inititalValues.put(BlacklistItemEntity.LASTINCOMINGSMS,
					entity.get_lastIncomingSMS());

			inititalValues.put(BlacklistItemEntity.LASTINCOMINGMMS,
					entity.get_lastIncomingMMS());

			inititalValues.put(BlacklistItemEntity.CANAUTOREPLY,
					entity.get_canAutoReply());

			boolean result = mDbAdapter.update(BlacklistItemEntity.TABLE_NAME,
					inititalValues, BlacklistItemEntity.PHONENUMBER + "=?",
					new String[] { entity.get_phoneNumber() }) > 0;

			return result;
		}
		return false;
	}

	public boolean deleteBlacklistItemEntity(BlacklistItemEntity entity) {
		return mDbAdapter.delete(BlacklistItemEntity.TABLE_NAME,
				BlacklistItemEntity.PHONENUMBER + "=?",
				new String[] { entity.get_phoneNumber() }) > 0;
	}

	public boolean deleteAllBlacklistItem() {
		return mDbAdapter.delete(BlacklistItemEntity.TABLE_NAME, null, null) > 0;
	}

	public BlacklistItemEntity[] getAllBlacklistItem() {

		BlacklistItemEntity[] blacklistitems = null;
		Cursor cur = mDbAdapter.query(BlacklistItemEntity.TABLE_NAME, null,
				null, null, null, null, null);
		if (cur != null) {
			blacklistitems = new BlacklistItemEntity[cur.getCount()];
			int i = 0;
			while (!cur.isAfterLast()) {
				blacklistitems[i++] = getBlacklistItemEntity(cur);
				cur.moveToNext();
			}
			cur.close();
		}
		return blacklistitems;
	}

	public BlacklistItemEntity getBlacklistItemByPhone(String phone) {

		BlacklistItemEntity blacklistitem = null;

		Cursor cur = mDbAdapter.query(BlacklistItemEntity.TABLE_NAME, null,
				BlacklistItemEntity.PHONENUMBER + "=?", new String[] { phone },
				null, null, null);

		if (cur != null) {
			blacklistitem = getBlacklistItemEntity(cur);
		}

		cur.close();

		return blacklistitem;
	}

	private BlacklistItemEntity getBlacklistItemEntity(Cursor cur) {
		if (cur != null) {
			BlacklistItemEntity values = new BlacklistItemEntity();

			values.set_bi_Name(cur.getString(cur
					.getColumnIndex(BlacklistItemEntity.BI_NAME)));

			values.set_phoneNumber(cur.getString(cur
					.getColumnIndex(BlacklistItemEntity.PHONENUMBER)));

			values.set_isBlockCall(cur.getInt(cur
					.getColumnIndex(BlacklistItemEntity.IS_BLOCKCALL)));

			values.set_isBlockMMS(cur.getInt(cur
					.getColumnIndex(BlacklistItemEntity.IS_BLOCKMMS)));

			values.set_isBlockSMS(cur.getInt(cur
					.getColumnIndex(BlacklistItemEntity.IS_BLOCKSMS)));

			values.set_isEnable(cur.getInt(cur
					.getColumnIndex(BlacklistItemEntity.IS_ENABLE)));

			values.set_canAutoReply(cur.getInt(cur
					.getColumnIndex(BlacklistItemEntity.CANAUTOREPLY)));

			values.set_creatingDate(cur.getString(cur
					.getColumnIndex(BlacklistItemEntity.CREATINGDATE)));

			values.set_note(cur.getString(cur
					.getColumnIndex(BlacklistItemEntity.NOTE)));

			values.set_lastIncomingCall(cur.getString(cur
					.getColumnIndex(BlacklistItemEntity.LASTINCOMINGCALL)));

			values.set_lastIncomingSMS(cur.getString(cur
					.getColumnIndex(BlacklistItemEntity.LASTINCOMINGSMS)));

			values.set_lastIncomingMMS(cur.getString(cur
					.getColumnIndex(BlacklistItemEntity.LASTINCOMINGMMS)));
			return values;
		}
		return null;
	}
}
